<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Video extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
     public function index()
    {
        $date=input();
        if($date){
            $where=[];
            if(!empty($date['title'])){
            $where[]=['title','=',$date['title']];
            }elseif(!empty($date['subject_id'])){
                $where[]=['subject_id','=',$date['subject_id']];
            }elseif(!empty($date['sort'])){
                $where[]=['sort','=',$date['sort']];
            }elseif(!empty($date['pink_strar_time'])&&!empty($date['pink_end_time'])){
                $date['pink_strar_time']=strtotime($date['pink_strar_time']);
                $date['pink_end_time']=strtotime($date['pink_end_timepink_end_time']); 
                $where[]=['pink_strar_time','=',$date['pink_strar_time']];
                $where[]=['pink_end_time','=',$date['pink_end_time']];
            }elseif(!empty($date['title'])&&!empty($date['classify_id'])&&!empty($date['sort'])&&!
                empty($date['pink_strar_time'])&&!empty($date['pink_end_time'])){
                $date['pink_strar_time']=strtotime($date['pink_strar_time']);
                $date['pink_end_time']=strtotime($date['pink_end_time']); 
                $where[]=['title','=',$date['title']];
                $where[]=['subject_id','=',$date['subject_id']];
                $where[]=['sort','=',$date['sort']];
                $where[]=['pink_strar_time','=',$date['pink_strar_time']];
                $where[]=['pink_end_time','=',$date['pink_end_time']];
            }
            $where['type']=3;
            $field='id,title,subject_id,image,is_pink,sort,sales,fake_sales,is_show';
            $list=db('eb_special')->field($field)->where($where)->paginate(2);
            $data=$list->items();
            foreach( $data as $k=>$v ){
            $data[$k]["subject"] = db( "eb_special_subject" )->where( ["id"=>$v['subject_id']] )->value( "name" );
            $data[$k]["sum"] = db( "eb_special_source" )->where( ["special_id"=>$v["id"]] )->count();
            $data[$k]["recommend"] = db( "eb_recommend_relation" )->where( ["link_id"=>$v["id"]] )->column( "recommend_id" );
             if(empty($data[$k]['recommend'])){
                       $data[$k]['recommend_name'][]=' ';
               }
            }
            foreach( $data as $kk=>$vv ){
                foreach( $vv['recommend'] as $rk=>$rv ){
                    $data[$kk]["recommend_name"][]= db( "eb_recommend" )->where( ["id"=>$rv] )->value( "title" );
                }
            }
             $lost=db('eb_special_subject')->select();
             $this->assign('lost',$lost);
             $this->assign("data",$data);
             $this->assign("list",$list);
              return $this->fetch();
        }else{
            $where=[];
            $where['type']=3;
            $field='id,title,subject_id,image,is_pink,sort,sales,fake_sales,is_show';
            $list=db('eb_special')->where($where)->field($field)->paginate(2);
            $data=$list->items();
           foreach( $data as $k=>$v ){
            $data[$k]["subject"] = db( "eb_special_subject" )->where( ["id"=>$v['subject_id']] )->value( "name" );
            $data[$k]["sum"] = db( "eb_special_source" )->where( ["special_id"=>$v["id"]] )->count();
            $data[$k]["recommend"] = db( "eb_recommend_relation" )->where( ["link_id"=>$v["id"]] )->column( "recommend_id" );
               if(empty($data[$k]['recommend'])){
                       $data[$k]['recommend_name'][]=' ';
               }
            }
            foreach( $data as $kk=>$vv ){
                foreach( $vv['recommend'] as $rk=>$rv ){
                    $data[$kk]["recommend_name"][]= db( "eb_recommend" )->where( ["id"=>$rv] )->value( "title" );
                  
                }
            }
             $lost=db('eb_special_subject')->select();
             $this->assign('lost',$lost);
             $this->assign("data",$data);
             $this->assign("list",$list);
             return $this->fetch();
        }
    }
   
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $list=db('eb_special_subject')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
     public  function upload()
     {
         $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = '6IeVuqWlzQ9f3Vz19UVOAbDDxmwjZpZdf8VId5F_';
        $secretKey = '5T3TJ_O0x1rUOGtaw4kktV91nYZqe-NefC1dACk-';
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = '1810php';

        //域名
        $domain = 'qutzb4nvh.hb-bkt.clouddn.com';
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }
       
     }
      public function getsu()
    {
        $name=input('title');
        if($name){
            $where=[];
            $where['type']=3;
            $where['title']=$name;
            $list=db('eb_special_task')->where($where)->paginate(3);
            $data=$list->items();
            $this->assign("data",$data);
            $this->assign("list",$list);
            return $this->fetch();
        }else{
        $where=[];
        $where['type']=3;
        $list=db('eb_special_task')->where($where)->paginate(3);
        $data=$list->items();
        $this->assign("data",$data);
        $this->assign("list",$list);
        return $this->fetch();
        }
    }
     public function lod()
    {
        $data=input();
        $id= implode(',',$data['data']);
        $where = [];
        $where[]= ["id","in",$id];
        $res=db('eb_special_task')->where($where)->select();
        if($res){
            return json(["code"=>0,"data"=>$res]);
        }else{
            return json(["code"=>1,"data"=>'']);
        }
    }
    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data=input();
         $list=str_replace(',','',$data['matter_id']);
        $data['sum']=strlen($list);
         $data['pink_strar_time']=strtotime($data['pink_strar_time']);
        $data['pink_end_time']=strtotime($data['pink_end_time']);
        $data['type']=3;
        $matter=$data['matter_id'];
        unset($data['matter_id']);
        unset($data['sum']);
        $res=db('eb_special')->insertGetId($data);
        if($res){
           $arr = explode( ',',$matter);
           $dataall = [];
           foreach( $arr as $k=>$v ){
                
                if( !empty( $v ) ){
                    $sdata = [];
                    $sdata['special_id'] = $res;
                    $sdata['pay_status'] = $data['pay_type'];
                    $sdata['source_id'] = $v;
                    $sdata['add_time']=time();
                    $dataall[] = $sdata;
                }
                
            }
            $loe=db('eb_special_source')->insertAll($dataall);
            if($loe){
                return $this->success('添加成功',url('video/index'));
            }else{
                return $this->error('添加失败');
            }
        }else{
             return $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $data=db('eb_special')->where('id',$id)->find();
        $data['pink_strar_time']=date('Y-m-d H:i:s',$data['pink_strar_time']);
        $data['pink_end_time']=date('Y-m-d H:i:s',$data['pink_end_time']);
        $matter_id=db('eb_special_source')->where('special_id',$data['id'])->value('source_id');
        $where = [];
        $where[]= ["id","in",$matter_id];
        $lost=db('eb_special_task')->where($where)->select();
        $list=db('eb_special_subject')->select();
        $this->assign('list',$list);
        $this->assign('lost',$lost);
        $this->assign("data",$data);
        return $this->fetch();
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
     public function edit()
    {
        $data=input();
        $data['type']=3;
        $matter=$data['matter_id'];
        unset($data['matter_id']);
        $res=db('eb_special')->where('id',$data['id'])->update($data);
        if($res){
            $special_id=$data['id'];
            $source_id=trim($matter, ",");
            $loe=db('eb_special_source')->where('Special_id',$special_id)->update(['source_id'=>$source_id]);
            if($loe){
                return $this->success('编辑成功',url('Video/index'));
            }else{
                return $this->error('编辑特失败');
            }
        }else{
             return $this->error('编辑十分失败');
        }
    }
    public function remove($id)
    {
       $res=db('eb_special')->where('id',$id)->delete();
        if ($res){
                return $this->success('删除成功',url('Video/index'));
        }else{
                return $this->error('删除失败');
        }
    }

  
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        $id=input('id');
        $where=[];
        $where['type']=1;
        $where['cid']=$id;
        $list=db('eb_store_pink')->where($where)->paginate(2);

        $data=$list->items();
        foreach ($data as $key => $value) {
            $data[$key]['cid_name']=db('eb_special')->where('id',$value['cid'])->value('title');
            $data[$key]['name']=db('eb_user')->where('id',$value['k_id'])->value('name');
        }
         $this->assign("data",$data);
         $this->assign("list",$list);
        return $this->fetch();
    }
     public function getsho($id)
    {
        $id=input('id');
        $list=db('recommend')->select();
        $this->assign("id",$id); 
        $this->assign('list',$list);
        return $this->fetch();
    }
      public function lof()
    {
        $inputdata=input();
        $data['type']=1;
        $data['recommend_id']=$inputdata['recommend'];
        $data['link_id']=$inputdata['id'];
        $data['sort']=$inputdata['sort'];
        $res=db('eb_recommend_relation')->insert($data);
        if ($res){
                return $this->success('推荐成功',url('Video/index'));
        }else{
                return $this->error('推荐失败');
        }
    }
     public function loe()
    {
        $id=input('id');
        $list=db('eb_special')->where('id',$id)->find();
        $list['pink_strar_time']=date('Y-m-d H:i:s',$list['pink_strar_time']);
        $list['pink_end_time']=date('Y-m-d H:i:s',$list['pink_end_time']);
        $this->assign('list',$list);
        return $this->fetch();

    }
    public function lor()
    {
        $data=input();
        $data['pink_strar_time']=strtotime($data['pink_strar_time']);
        $data['pink_end_time']=strtotime($data['pink_end_time']);
        $res=db('eb_special')->where('id',$data['id'])->update(["pink_money"=>$data['pink_money'],"pink_number"=>$data['pink_number'],"pink_time"=>$data['pink_time'],
            'pink_strar_time'=>$data['pink_strar_time'],"pink_end_time"=>$data['pink_end_time'],"is_fake_pink"=>$data['is_fake_pink'],"fake_pink_number"=>$data['fake_pink_number']]);
        if ($res){
                return $this->success('设置成功',url('Video/index'));
        }else{
                return $this->error('设置失败');
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
